package 代理模式;

import sun.misc.ProxyGenerator;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Proxy;

/**
 * @author zzs
 * @ClassName Demo.java
 * @Description 对对象的访问进行了控制，由代理类来实现，这样做的好处可以让目标类更专心做自己的事情也就是单一职责，让调用者和主题者之间进行了松耦合
 * 也可以将共同冗余的功能进行整合处理
 * @createTime 2022年05月19日 20:07:00
 */
public class Demo {
    public static void main(String[] args) {
        Student student = (Student) Proxy.newProxyInstance(OneStudent.class.getClassLoader(), OneStudent.class.getInterfaces(), new StudentProxy(new OneStudent()));
        Student student2 = (Student) Proxy.newProxyInstance(OneStudent.class.getClassLoader(), OneStudent.class.getInterfaces(), new StudentProxy(new OneStudent()));
        System.out.println(student2 == student);
        student.running();
        byte[] bytes= ProxyGenerator.generateProxyClass("$Proxy0",new Class[]{Student.class});
        try {
            FileOutputStream fs = new FileOutputStream(new File("D:\\$Proxy0.class"));
            fs.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
